<?php

namespace app\models;

use common\libs\Tools;
use Yii;

/**
 * This is the model class for table "pay_application".
 *
 * @property int $aid
 * @property string $merchants 商户号
 * @property string $a_key 应用key
 * @property string $name 应用名称
 * @property string $remake 备注
 * @property string $status 1-启用，2-停止
 * @property string $create_time 创建时间
 */
class Application extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'pay_application';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['app_id', 'a_key', 'name', 'remake', 'create_time'], 'required'],
            [['status'], 'string'],
            [['create_time'], 'safe'],
            [['merchants'], 'string', 'max' => 10],
            [['a_key', 'name'], 'string', 'max' => 32],
            [['remake'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'aid' => 'Aid',
            'app_id' => 'App Id',
            'a_key' => 'A Key',
            'name' => 'Name',
            'remake' => 'Remake',
            'status' => 'Status',
            'create_time' => 'Create Time',
        ];
    }


    public function addLicat($app_id,$name)
    {
        $paramsData = [
            'Application' => [
                'app_id' => $app_id,
                'a_key' => Tools::generateStr(32),
                'name' => $name,
                'remake' => '',
                'status' => '1',
                'create_time' => date("Y-m-d H:i:s"),
            ],
        ];

        if ($this->load($paramsData) and $this->validate()){
            if ($this->save(0)){
                return true;
            }
        }
        return false;
    }



    /*** 添加、修改
     * @param $data
     * @return bool
     */
    public function addInfo($data)
    {
        if (isset($data['aid']) and !empty($data['aid'])){
            $result = $this->modInfo($data);
            if ($result){
                return true;
            }else{
                return false;
            }
        }

        $model = new self();
        $model->app_id = isset($data['app_id'])?$data['app_id']:'';
        $model->name = isset($data['name'])?$data['name']:'';
        $model->remake = isset($data['remake'])?$data['remake']:'';
        $model->status = isset($data['status'])?$data['status']:1;
        $model->create_time = date("Y-m-d H:i:s");

        $model->a_key = Tools::createStr(32);
        $model->aes_key = Tools::createStr(16);
        $model->aes_iv = Tools::createStr(16);

        if ($model->save(0)){
            return true;
        }

        return  false;
    }


    public function modInfo($data)
    {
        $model = self::find()->where(['aid' => $data['aid']])->one();
        $model->name = isset($data['name'])?$data['name']:'';
        $model->remake = isset($data['remake'])?$data['remake']:'';
        $model->status = isset($data['status'])?$data['status']:'';
        if ($model->save(0)){
            return true;
        }

        return false;

    }






}
